var from_search = false;
var lineItemReadOnlycolumnDefs = [ {
	key : "orderLineItemNumber",
	label : "Line",
	width : "50px"
	
}, {
	key : "partNumber",
	label : "Part Number",
	width : "50px"
	
},{
	key : "lineDescription",
	label : "Description",
	width : "50px"
}, {
	key : "quantity",
	label : "Quantity",
	width : "50px"
	
} ,{
	key : "uomSymbol",
	label : "Unit Of Measure",
	width : "50px"
} ,{
	key : "unitPrice",
	label : "Unit Price",
	width : "50px"
} /*,{
	key : "currency",
	label : "Currency",
	width : "50px"
}*/ ,{
	key : "lineAmount",
	label : "Line Total",
	width : "50px"
}  ];

var splitItemReadOnlyColDefs = 			[ {
	key : "orderSplitLineItemNumber",
	label : "Split-Line#",
	width : "50px"
	
}, {
	key : "lineSplitPercent",
	label : "Split %",
	width : "50px"
}, {
	key : "glAccountNumber",
	label : "GL Account",
	width : "50px"
},{
	key : "costCenterNumber",
	label : "Cost Center",
	width : "50px"
	
} ,{
	key : "projectNumber",
	label : "Project",
	width : "50px"
} ,{
	key : "wbsElementNumber",
	label : "WBS Element",
	width : "50px"
}  ,
{
	key : "parentId",
	hidden: true
}];
var podetail_tabview = null;
var sel_po_cache = null;
function open_sel_po(ponum){
	from_search = false;
	u_hideAllSections();
	u_safeShowElement('podetails_div');
	
	/*if(podetail_tabview == null){
		podetail_tabview = new YAHOO.widget.TabView("podetails_div_inner");
	}
	
	podetail_tabview.set('activeIndex',0);*/
	try{
	domtab.showTabByName("aorderDetailTab");
	}catch(ex){}
	
	var po = getpofrom_cache(ponum);
	sel_po_cache = po;
	if(!isEmptyString(po.orderCompany)){
		find_ordercomp(po.orderCompany);
		dwr.util.setValue('po_buyer_name',po.clientNameText);
		dwr.util.setValue('po_supp_name',"");
	}
	if(!isEmptyString(po.orderSupplier)){
		find_ordersupplier(po.orderSupplier);
		dwr.util.setValue('po_buyer_name',"");
		dwr.util.setValue('po_supp_name',po.clientNameText);
	}
	
	populate_invoiceForSelectedPO(po);
	
	populate_attachmentForSelectedPO(po);
	
	populate_commentsForSelectedPO(po);
	//alert(isNotFromDashboard);
	if(isNotFromDashboard){
		
		render_editableLineitems(po);
		
	} else{
			render_lineitems(po)
	}

	try{
	var paymentTermObj = getPaymentTerm(sel_po_cache.orderPaymentTermId);
	$("po_display_paymentTerm").innerHTML = paymentTermObj.desc;
	}catch(ex){
		$("po_display_paymentTerm").innerHTML = "No Payment Term";
	}
	
	if(posent_cache && posent_cache != null){
		for(var i=0;i<posent_cache.length;i++){
			
			if(sel_po_cache.orderNumber == posent_cache[i].orderNumber){
				
				$("po_updateAction").style.display = "block";
				assignAttachmentComponent("attachmentComponetPlaceHolder2");
				assignCommentsComponent("commentComponetPlaceHolder2");
				dwr.util.setValue('target_comp_comment',"UPDATE_PO");
				dwr.util.setValue('target_comp_attachment',"UPDATE_PO");
				$("attph2_hdr").style.display = "block";
				
				$("attcm2_hdr").style.display = "block";
				break;
			} else{
				$("po_updateAction").style.display = "none";
				$("attph2_hdr").style.display = "none";
				$("attcm2_hdr").style.display = "none";
			}
		}
	}else{
		$("po_updateAction").style.display = "none";
		$("attph2_hdr").style.display = "none";
		$("attcm2_hdr").style.display = "none";
	}
	
	//alert(ponum);
	//show po using po num
	dwr.util.setValue('podetails_ponum',ponum);
	//;
	dwr.util.setValue('podetails_version',po.versionNumber);
	dwr.util.setValue('podetails_amt',po.orderAmount);
	dwr.util.setValue('podetails_dcname1',po.orderDeliveryContact);
	dwr.util.setValue('podetails_dcphone',po.orderContactPhone);
	dwr.util.setValue('podetails_dcemail',po.orderContactEmail);
	
	
	
	if(po.orderDate != null){
		dwr.util.setValue('podetails_subdate',YAHOO.util.Date.format(po.orderDate,{ format: "%m/%d/%Y %I:%M %P"}));
		dwr.util.setValue('podetails_recddate',YAHOO.util.Date.format(po.orderDate,{ format: "%m/%d/%Y %I:%M %P"}));
	}
	
	create_poh_results_table();
	clear_poh_results_table();
	populate_poh_table(po.poHistory);
	
	if(!isNavigationFromBackButton){
	var functionPointerObj = new Object();
	functionPointerObj.functionName = "open_sel_po("+"'"+ponum+"'"+")";
	navigationStateArray.push(functionPointerObj);
	}
	
}
var pod_litems_yui_instance = null;

function clearlineitemsRoTable(){
	if(pod_litems_yui_instance != null && pod_litems_yui_instance.getRecordSet() != null && pod_litems_yui_instance.getRecordSet().getLength() > 0){
		pod_litems_yui_instance.deleteRows(0,pod_litems_yui_instance.getRecordSet().getLength());
	}
}

function render_editableLineitems(po){
	$("podetails_lineitem_yui_table").style.display = "none";
	//var liDataSource = new YAHOO.util.DataSource();
	//liDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
	//pod_litems_yui_instance = new YAHOO.widget.DataTable("podetails_lineitem_yui_table",lineItemReadOnlycolumnDefs,liDataSource,{});
	assignNestedDataTableComp("lineItem_Table1_PlaceHolder2");
	createNestedDataTable();
	clearnestedDataTable();
	nesteddataTable.set("selectionMode", "single");
	nesteddataTable.subscribe("rowClickEvent",			nesteddataTable.onEventSelectRow);
	nesteddataTable.subscribe("nestedCreateEvent",function(oArgs){
		var po = sel_po_cache;
		
		if(!po || po == null || (oArgs.dt.getRecordSet() != null && oArgs.dt.getRecordSet().getLength() > 0 )){
			return;
		}
		var id = oArgs.td.parentNode.id;
		var rec = nesteddataTable.getRecordSet().getRecord(id);
		try{
			for(var i=0;i<po.orderLineItems.length;i++){
				if(po.orderLineItems[i].lineItemSplits != null && po.orderLineItems[i].lineItemSplits.length > 0){
					if(rec.getData("orderLineItemNumber") == po.orderLineItems[i].orderLineItemNumber){
						oArgs.dt.addRows(po.orderLineItems[i].lineItemSplits);
						//oArgs.dt.render();
						break;
					}
				}
			}
		}catch(ex){}
		
	});
	
	
	nesteddataTable.addRows(po.orderLineItems);
	nesteddataTable.render();
	//setTimeout("render_splits()",4000);
}


function render_lineitems(po){
	$("lineItem_Table_comp").style.display = "none";
	var liDataSource = new YAHOO.util.DataSource();
	liDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
	//pod_litems_yui_instance = new YAHOO.widget.DataTable("podetails_lineitem_yui_table",lineItemReadOnlycolumnDefs,liDataSource,{});
	createNestedDataReadOnlyTable();
	clearlineitemsRoTable();
	pod_litems_yui_instance.set("selectionMode", "single");
	pod_litems_yui_instance.subscribe("rowClickEvent",
			pod_litems_yui_instance.onEventSelectRow);
	pod_litems_yui_instance.subscribe("nestedCreateEvent",function(oArgs){
		var po = sel_po_cache;
		
		if(!po || po == null || (oArgs.dt.getRecordSet() != null && oArgs.dt.getRecordSet().getLength() > 0 )){
			return;
		}
		var id = oArgs.td.parentNode.id;
		var rec = pod_litems_yui_instance.getRecordSet().getRecord(id);
		for(var i=0;i<po.orderLineItems.length;i++){
			if(po.orderLineItems[i].lineItemSplits != null && po.orderLineItems[i].lineItemSplits.length > 0){
				if(rec.getData("orderLineItemNumber") == po.orderLineItems[i].orderLineItemNumber){
					oArgs.dt.addRows(po.orderLineItems[i].lineItemSplits);
					//oArgs.dt.render();
					break;
				}
			}
		}
		
	});
	pod_litems_yui_instance.addRows(po.orderLineItems);
	pod_litems_yui_instance.render();
	//setTimeout("render_splits()",4000);
}


function render_splits(){
	var po = sel_po_cache;
	
	if(!po || po == null){
		return;
	}
	for(var i=0;i<po.orderLineItems.length;i++){
		var rcs = pod_litems_yui_instance.getRecordSet().getRecords();
		if(po.orderLineItems[i].lineItemSplits != null && po.orderLineItems[i].lineItemSplits.length > 0){
			for(var j=0;j<rcs.length;j++){
				if(rcs[j].getData("orderLineItemNumber") == po.orderLineItems[i].orderLineItemNumber){
					addEmptySplitReadOnlyRow(rcs[j].getId(),po.orderLineItems[i].lineItemSplits);
					break;
				}
			}
		}
	}
	
	pod_litems_yui_instance.render();
}


function find_ordercomp(cid){
	var dcmd = {
			callback : success_setordercomp,
			exceptionHandler : exception_generic_handler,
			timeout : 100000,
			errorHandler : exception_generic_handler	
	};
	
	DWR_PurchaseOrders.findCompanyById(cid,
			dcmd);
}

function find_ordersupplier(cid){
	var dcmd = {
			callback : success_setordersupp,
			exceptionHandler : exception_generic_handler,
			timeout : 100000,
			errorHandler : exception_generic_handler	
	};
	
	DWR_PurchaseOrders.findCompanyById(cid,
			dcmd);
}

var podetails_ordercomp = null;
var podetails_ordersupp = null;

function success_setordercomp(dwrresp){
	if (dwrresp && dwrresp.respCode == 0) {
		if (dwrresp.objRespOrError != null
				&& !isEmptyString(dwrresp.objRespOrError)) {
			podetails_ordercomp = dwrresp.objRespOrError;
			var addr = podetails_ordercomp.addressLine1 + "<br>" + podetails_ordercomp.addressLine2 + "<br>" + podetails_ordercomp.city + "<br>" + podetails_ordercomp.state + ", " + podetails_ordercomp.zip;
			dwr.util.setValue('podetails_baddrl1',addr,{escapeHtml : false});
			dwr.util.setValue('podetails_saddrl1',addr,{escapeHtml : false});
		}
	}
}

function success_setordersupp(dwrresp){
	if (dwrresp && dwrresp.respCode == 0) {
		if (dwrresp.objRespOrError != null
				&& !isEmptyString(dwrresp.objRespOrError)) {
			podetails_ordersupp = dwrresp.objRespOrError;
		}
	}
}

function getpofrom_cache(ponum){
	if(search_found_cache && search_found_cache != null && search_found_cache.documentType == "PO"){
		for(var i=0;i<search_found_cache.length;i++){
			if(search_found_cache[i].orderNumber == ponum){
				from_search = true;
				return search_found_cache[i];
			}
		}
	}
	
	if(poreceived_cache && poreceived_cache != null){
		for(var i=0;i<poreceived_cache.length;i++){
			if(poreceived_cache[i].orderNumber == ponum){
				return poreceived_cache[i];
			}
		}
	}
		
	if(posent_cache && posent_cache != null){
		for(var i=0;i<posent_cache.length;i++){
			if(posent_cache[i].orderNumber == ponum){
				return posent_cache[i];
			}
		}
	}
	
	
		
	return null;
}

function createNestedDataReadOnlyTable(){
	if(pod_litems_yui_instance == null){
		var liDataSource = new YAHOO.util.DataSource();
		liDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
		pod_litems_yui_instance = new YAHOO.widget.NestedDataTable(
			'podetails_lineitem_yui_table', 
			lineItemReadOnlycolumnDefs,
			liDataSource,
			splitItemReadOnlyColDefs,
			splitItemDataSource,
			{	
				width: "600px",
				
				// we'll leave the actual loading for later
				initialLoad:false, 
				
				// This function links the master to the nested tables
				generateNestedRequest: function (record) {
					return splitEmptyData;

				}
			}
			
			
			
			// configuration attributes for the nested tables would go here
		);
		
		
	}
} 

function addEmptySplitReadOnlyRow(oRecordId,splitData){
	try{
		pod_litems_yui_instance.getRecordSet().getRecord(oRecordId)._oData.__NESTED__.dt.addRows(splitData);
	}catch(ex){
		alert(ex);
	}
}

var invoiceForSelectedPO_yui_table_instance = null;

function populate_invoiceForSelectedPO(po){
	try{
	create_invoiceForSelectedPO();
	clear_invoiceForSelectedPO();
	var slectedInvoiceArray = po.orderInvoices;
	var slectedInvoiceObjectArray = new Array();
	for(var i=0;i<slectedInvoiceArray.length;i++){
		var invoiceID = slectedInvoiceArray[i];
		if(inv_recdcache && inv_recdcache != null){
			for(var j=0;j<inv_recdcache.length;j++){
				if(invoiceID == inv_recdcache[j].invoiceNumber){
					slectedInvoiceObjectArray.push(inv_recdcache[j]);
				}
			}
		}
		
		if(invsent_cache && invsent_cache != null){
			for(var k=0;k<invsent_cache.length;k++){
				if(invoiceID == invsent_cache[k].invoiceNumber){
					slectedInvoiceObjectArray.push(invsent_cache[k]);
				}
			}
		}
	}
	
	invoiceForSelectedPO_yui_table_instance.addRows(slectedInvoiceObjectArray);
	} catch(ex){
		create_invoiceForSelectedPO();
		clear_invoiceForSelectedPO();
	}
}

function populate_attachmentForSelectedPO(po){
	try{
	dwr.util.removeAllRows("poDisplayattachments_tbody", {
		filter : function(tr) {
			return (tr.id != "poDisplayattachments_pattern");
		}
	});	
	if(po.orderAttachmentsId != null){
	
		for(var i=0;i<po.orderAttachmentsId.length;i++){
			var responseText = po.orderAttachmentsId[i];
			var ids = i + ""
			dwr.util.cloneNode('poDisplayattachments_pattern', {idSuffix : ids});
			//dwr.util.setValue('poaattach_id'+i,o.responseText); //hidden field
			$("poDispalyattachments_link"+ids).setAttribute("href",'javascript:openAttachmentPanel("http://www.atsocii.com'+responseText.split(":")[1]+'");');
			$("poDispalyattachments_link"+ids).innerHTML = responseText.split(":")[3];
			$("poDisplayattachments_pattern"+ids).style.display = '';
		}
	}
	}catch(ex){}
}

function populate_commentsForSelectedPO(po){
	
	try{
		dwr.util.removeAllRows("poDisplaycomments_tbody", {
			filter : function(tr) {
				return (tr.id != "poDisplaycomments_pattern");
			}
		});
		
		if(po.orderComments != null){
		
			for(var i=0;i<po.orderComments.length;i++){
				var responseText = po.orderComments[i];
				var ids = i + ""
				dwr.util.cloneNode('poDisplaycomments_pattern', {idSuffix : ids});
				$("poDisplaycomment_text"+ids).innerHTML = responseText;
				$("poDisplaycomments_pattern"+ids).style.display = '';
			}
		}
		}catch(ex){}
}


function create_invoiceForSelectedPO() {
	if(invoiceForSelectedPO_yui_table_instance == null){
	var invrcolumnDefs = [ {
		key : "invoiceNumber",
		label : "Invoice#",
		sortable : true,
		formatter : "hlinkFormatter"
	}, {
		key : "supplierNameText",
		label : "Supplier",
		sortable : true
	}, {
		key : "invoiceSubmittedDate",
		label : "Invoice Date",
		formatter: "date",
		sortable : true
	} , {
		key : "purchaseOrderNumber",
		label : "PO#"
	}, {
		key : "totalInvoiceAmount",
		label : "Amount",
		formatter: "currency"
	} ];

	var invrDataSource = new YAHOO.util.DataSource();
	invrDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
	 var oConfigs = {
             paginator: new YAHOO.widget.Paginator({
                 rowsPerPage: 2
                 
             }),
             initialRequest: "results=504",
             width: "600px",
             // we'll leave the actual loading for later
 			initialLoad:true, 
 				
 			// This function links the master to the nested tables
 			generateNestedRequest: function (record) {
 				return lineItemData;

 			}
     };
	 /*por_yui_table_instance = new YAHOO.widget.NestedDataTable(
				'poreceived_yui_table', 
				porcolumnDefs,
				porDataSource,
				lineItemReadOnlycolumnDefs,
				lineItemDataSource,
				oConfigs);*/
	 invoiceForSelectedPO_yui_table_instance = new YAHOO.widget.DataTable("podetails_invoice_yui_table",invrcolumnDefs,invrDataSource,oConfigs);
	 invr_yui_table_instance.set("selectionMode", "single");
	 invr_yui_table_instance.subscribe("rowClickEvent",
			 invr_yui_table_instance.onEventSelectRow);
	//por_yui_table_instance.subscribe("nestedToggleEvent",additemsr_tonested); 
	}

}

function clear_invoiceForSelectedPO() {
	if (invoiceForSelectedPO_yui_table_instance != null && invoiceForSelectedPO_yui_table_instance.getRecordSet() != null && invoiceForSelectedPO_yui_table_instance.getRecordSet().getLength() > 0) {
		var length = invoiceForSelectedPO_yui_table_instance.getRecordSet().getLength();
		if (length > 0) {
			invoiceForSelectedPO_yui_table_instance.deleteRows(0, length);
		}
	}
}

function assignAttachmentComponent(domElementName){
	YAHOO.util.Dom.insertAfter ( "attachmentComponent" , domElementName) ;
	dwr.util.removeAllRows("poattachments_tbody", {
		filter : function(tr) {
			return (tr.id != "poattachments_pattern");
		}
	});
	attachmentctr = 0;
	var dtt = new Date();
	var ts = dtt.getTime() +  "";
	dwr.util.setValue('target_comp_attachment_id',ts);
	$('attachmentComponent').style.display='block';
}

function assignCommentsComponent(domElementName){
	try{
	YAHOO.util.Dom.insertAfter ( "commentsComponent" , domElementName) ;
	dwr.util.removeAllRows("pocomments_tbody", {
		filter : function(tr) {
			return (tr.id != "pocomments_pattern");
		}
	});
	pocomments_ctr = 0;
	var dtt = new Date();
	var ts = dtt.getTime() +  "";
	dwr.util.setValue('target_comp_comment_id',ts);
	$('commentsComponent').style.display='block';
	}catch(ex){}
}

function updatePO(){
	
	try{
		var poNewAtchArr = new Array();
		if(attachmentctr > 0){
			for(var ip=0;ip<attachmentctr;ip++){
				if($('poattachments_check'+ip+'').checked){
					poNewAtchArr.push(dwr.util.getValue('poaattach_id'+ip+''));
					
				}
			}
		}
		
		
		
		
		var poNewCmtarr = new Array();
		if(pocomments_ctr > 0){
			for(var iq=0;iq<pocomments_ctr;iq++){
				if($('pocomment_check'+iq+'').checked){
					poNewCmtarr.push(dwr.util.getValue('pocomment_text'+iq+''));
				}
			}
		}
		
		
	}catch(ex){}
	
	var dwrCallMetaData = {
			callback : success_updatepo,
			exceptionHandler : exception_updatepo_handler,
			timeout : 100000,
			errorHandler : exception_generic_handler
		};
	sel_po_cache  = updateObjectWithLineItem(sel_po_cache,sel_po_cache.orderNumber);//TODO:Add backend code.
	//DWR_PurchaseOrders.updatePO(sel_po_cache.orderNumber,poNewAtchArr,poNewCmtarr,loggedinuser,dwrCallMetaData);
	for(var i=0;i<poNewAtchArr.length;i++){
		if(sel_po_cache != null){
			if(sel_po_cache.orderAttachmentsId == null){
				sel_po_cache.orderAttachmentsId = new Array();
			}
			sel_po_cache.orderAttachmentsId.push(poNewAtchArr[i]);
		}
	}
	
	for(var i=0;i<poNewCmtarr.length;i++){
		if(sel_po_cache != null){
			if(sel_po_cache.orderComments == null){
				sel_po_cache.orderComments = new Array();
			}
			sel_po_cache.orderComments.push(poNewCmtarr[i]);
		}
	}
	DWR_PurchaseOrders.createOrSavePurchaseOrder(sel_po_cache, false, dwrCallMetaData);
	
}


function cancelPO(){
	
	var isDel = confirm("Are You Sure You Want to Cancel PO?");
	
	if(isDel){
		var dwrCallMetaData = {
				callback : success_cancelpo,
				exceptionHandler : exception_updatepo_handler,
				timeout : 100000,
				errorHandler : exception_generic_handler
			};
		//sel_po_cache  = updateObjectWithLineItem(sel_po_cache,sel_po_cache.orderNumber);//TODO:Add backend code.
		DWR_PurchaseOrders.cancelPO(sel_po_cache.orderNumber,loggedinuser,dwrCallMetaData);
	}
	
}

function success_cancelpo(dwrresp){
	if (dwrresp && dwrresp.respCode == 0) {
		alert("PO Cancelled.");
	}
}


function success_updatepo(dwrresp){
	if (dwrresp && dwrresp.respCode == 0) {
		if (dwrresp.objRespOrError != null
				&& !isEmptyString(dwrresp.objRespOrError)) {
			var ponum = dwrresp.objRespOrError.orderNumber;
			if(posent_cache && posent_cache != null){
				for(var i=0;i<posent_cache.length;i++){
					if(posent_cache[i].orderNumber == ponum){
						posent_cache[i] = dwrresp.objRespOrError ;
						break;
					}
				}
			}		
			open_sel_po(ponum);
			alert("Purchase Order Updated.");
		}
	} else{
		alert("Purchase Order could not be Updated. Please try again.");
	}
}

function exception_updatepo_handler(resp){
	//console.log(resp);
}

var formatLastUpdateDate = function(elCell, oRecord, oColumn, oData) {
	var sFormat = "%m/%d/%Y %I:%M %P";

	var str = YAHOO.util.Date.format(oData, {format: sFormat});
	
	elCell.innerHTML = str;
}
var poh_yui_table_instance = null;
var pohcolumnDefs = [ {
	key : "orderNumber",
	label : "Order#"
}, {
	key : "versionNumber",
	label : "Order Version",
	formatter: "hlinkFormatter"	
},{
	key : "clientNameText",
	label : "Supplier",
	sortable : true
}, {
	key : "lastModifiedDate",
	label : "Last Modified Date",
	formatter: formatLastUpdateDate,
	sortable : true
}, {
	key : "lastModifiedBy",
	label : "Last Modified By",
	formatter: "date",
	sortable : true
} , {
	key : "orderAmount",
	label : "Order Amount",
	formatter: "currency"
}];

function create_poh_results_table() {
	if(poh_yui_table_instance == null){
		var posDataSource = new YAHOO.util.DataSource();
		posDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
		poh_yui_table_instance = new YAHOO.widget.DataTable("po_history_yui_table",pohcolumnDefs,posDataSource,{});
		poh_yui_table_instance.set("selectionMode", "single");
		poh_yui_table_instance.subscribe("rowClickEvent",poh_yui_table_instance.onEventSelectRow);
	}
}

function clear_poh_results_table() {
	if (poh_yui_table_instance != null) {
		var length = poh_yui_table_instance.getRecordSet().getLength();
		if (length > 0) {
			poh_yui_table_instance.deleteRows(0, length);
		}
	}
}

function populate_poh_table(history){
	if(history != undefined && history != null && history.length > 0){
		for(var i=0;i<history.length;i++){
			history[i].clientNameText = sel_po_cache.clientNameText;
		}
		poh_yui_table_instance.addRows(history);
	}
}

function getpoverfrom_cache(ver){
	var ret = null;
	if(sel_po_cache.poHistory && sel_po_cache.poHistory != null && sel_po_cache.poHistory.length > 0){
		for(var idx=0; idx<sel_po_cache.poHistory.length;idx++){
			if(sel_po_cache.poHistory[idx].versionNumber == ver){
				ret = sel_po_cache.poHistory[idx];
				break;
			}
		}
	}
	
	return ret;
}

function open_sel_po_ver(ver){
	u_hideAllSections();
	u_safeShowElement('podetails_div');
	
	/*if(podetail_tabview == null){
		podetail_tabview = new YAHOO.widget.TabView("podetails_div_inner");
	}*/
	
	//podetail_tabview.set('activeIndex',0);
	domtab.showTabByName("aorderDetailTab");
	
	var po = getpoverfrom_cache(ver);
	
	if(!isEmptyString(po.orderCompany)){
		find_ordercomp(po.orderCompany);
		dwr.util.setValue('po_buyer_name',po.clientNameText);
		dwr.util.setValue('po_supp_name',"");
	}
	if(!isEmptyString(po.orderSupplier)){
		find_ordersupplier(po.orderSupplier);
		dwr.util.setValue('po_buyer_name',"");
		dwr.util.setValue('po_supp_name',po.clientNameText);
	}
	
	render_lineitems(po);
	populate_invoiceForSelectedPO(po);
	populate_attachmentForSelectedPO(po);
	populate_commentsForSelectedPO(po);
	
	try{
	var paymentTermObj = getPaymentTerm(po.orderPaymentTermId);
		$("po_display_paymentTerm").innerHTML = paymentTermObj.desc;
	}catch(ex){
		$("po_display_paymentTerm").innerHTML = "No Payment Term";
	}
	
	if(posent_cache && posent_cache != null){
		for(var i=0;i<posent_cache.length;i++){
			if(po.orderNumber == posent_cache[i].orderNumber
					&& po.versionNumber == sel_po_cache.versionNumber){
				$("po_updateAction").style.display = "block";
				assignAttachmentComponent("attachmentComponetPlaceHolder2");
				assignCommentsComponent("commentComponetPlaceHolder2");
				dwr.util.setValue('target_comp_comment',"UPDATE_PO");
				dwr.util.setValue('target_comp_attachment',"UPDATE_PO");
				$("attph2_hdr").style.display = "block";
				$("attcm2_hdr").style.display = "block";
				break;
			} else{
				$('attachmentComponent').style.display='none';
				$('commentsComponent').style.display='none';
				$("po_updateAction").style.display = "none";
				$("attph2_hdr").style.display = "none";
				$("attcm2_hdr").style.display = "none";
			}
		}
	}else{
		$('attachmentComponent').style.display='none';
		$('commentsComponent').style.display='none';
		$("po_updateAction").style.display = "none";
		$("attph2_hdr").style.display = "none";
		$("attcm2_hdr").style.display = "none";
	}
	
	
	//show po using po num
	dwr.util.setValue('podetails_ponum',po.orderNumber);
	dwr.util.setValue('podetails_version',po.versionNumber);
	dwr.util.setValue('podetails_amt',po.orderAmount);
	dwr.util.setValue('podetails_dcname1',po.orderDeliveryContact);
	dwr.util.setValue('podetails_dcphone',po.orderContactPhone);
	dwr.util.setValue('podetails_dcemail',po.orderContactEmail);
	
	
	
	if(po.orderDate != null){
		dwr.util.setValue('podetails_subdate',YAHOO.util.Date.format(po.orderDate,{ format: "%m/%d/%Y %I:%M %P"}));
		dwr.util.setValue('podetails_recddate',YAHOO.util.Date.format(po.orderDate,{ format: "%m/%d/%Y %I:%M %P"}));
	}
}

function showpoxml(){
	showxmldata(poxml_to_open);
}

function showasxml_po(){
	var data = "";
	if(sel_po_cache && sel_po_cache != null){
		data = sel_po_cache.orderXml;
	}
	showxmldata(data);
}
function assignNestedDataTableComp(domElementName){
	try{
	$("lineItem_Table_comp").style.display = "block";
	YAHOO.util.Dom.insertAfter ( "lineItem_Table_comp" , domElementName) ;
	}catch(ex){}
}